package com.ruoyi.back.service.impl

import cn.hutool.core.bean.BeanUtil
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.back.domain.UserSystemNoticeMessage
import com.ruoyi.back.enums.SendUserTypeEnum
import com.ruoyi.back.enums.UserTypeEnum
import com.ruoyi.back.mapper.UserSystemNoticeMessageMapper
import com.ruoyi.back.service.AppUserService
import com.ruoyi.back.service.SystemNoticeParam
import com.ruoyi.back.service.UserSystemNoticeMessageService
import com.ruoyi.socket.WebSocketServer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class UserSystemNoticeMessageServiceImpl : ServiceImpl<UserSystemNoticeMessageMapper, UserSystemNoticeMessage>(),
    UserSystemNoticeMessageService {

    @Autowired
    private lateinit var appUserService: AppUserService

    override fun sendUserSystemNoticeMessage(sendUserType: SendUserTypeEnum, systemNoticeParam: SystemNoticeParam) {
        systemNoticeParam.run {
            when (sendUserType) {
                SendUserTypeEnum.ALL_USER -> appUserService.getAppUserIdByUserType(null)?.let {
                    it.forEach {
                        WebSocketServer.sendMessage(it)
                    }
                    send(it)
                }

                SendUserTypeEnum.MEMBER_USER -> appUserService.getAppUserIdByUserType(UserTypeEnum.MEMBER)?.let {
                    it.forEach {
                        WebSocketServer.sendMessage(it)
                    }
                    send(it)
                }

                SendUserTypeEnum.NO_MEMBER_USER -> {
                    appUserService.getAppUserIdByUserType(UserTypeEnum.NORMAL)?.let {
                        it.forEach {
                            WebSocketServer.sendMessage(it)
                        }
                        send(it)
                    }
                    appUserService.getAppUserIdByUserType(UserTypeEnum.EXPIRE_MEMBER)?.let {
                        it.forEach {
                            WebSocketServer.sendMessage(it)
                        }
                        send(it)
                    }
                }

                SendUserTypeEnum.SINGLE_USER -> {
                    systemNoticeParam.singleUserId?.run {
                        WebSocketServer.sendMessage(this)
                        send(listOf(this))
                    }

                }
            }
        }
    }

    val saveUserMessage: (SystemNoticeParam, List<Long>?) -> Boolean = { systemNoticeParam, userIds ->
        userIds?.run {
            saveBatch(userIds.map {
                UserSystemNoticeMessage().apply {
                    BeanUtil.copyProperties(systemNoticeParam, this)
                    createBy = it
                }
            })
        } ?: kotlin.run {
            save(UserSystemNoticeMessage().apply {
                BeanUtil.copyProperties(systemNoticeParam, this)
                createBy = systemNoticeParam.singleUserId
            })
        }
    }

    fun SystemNoticeParam.send(userIds: List<Long>?) = saveUserMessage(this, userIds)
}